package com.ihr360.commons.entityhandler;

import com.ihr360.commons.constant.Constants;
import com.ihr360.commons.context.session.Ihr360Session;
import com.ihr360.commons.context.session.Ihr360SessionContextHolder;
import com.ihr360.commons.entity.AuthUserInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.StringUtils;

import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;

public class Ihr360ContextHandler {
    Logger logger = LoggerFactory.getLogger(Ihr360ContextHandler.class);

    @PrePersist
    @PreUpdate
    void onPrePersist(Object o) {
        if (o == null) {
            return;
        }
        final BeanWrapper beanWrapper = new BeanWrapperImpl(o);

        PropertyDescriptor[] descriptors = beanWrapper.getPropertyDescriptors();
        for (PropertyDescriptor descriptor:descriptors){
            if (descriptor.getName().equals(Constants.Session.COMPANY_ID)){
                //对于已经进行了赋值的companyId,不进行重新赋值操作
                try {
                    if (!StringUtils.isEmpty(BeanUtils.getProperty(o, Constants.Session.COMPANY_ID))) {
                        continue;
                    }
                }catch (InvocationTargetException e1){
                    if (logger.isWarnEnabled()){
                        logger.warn(e1.getMessage(),e1);
                    }
                }catch (NoSuchMethodException e2) {
                    if (logger.isWarnEnabled()){
                        logger.warn(e2.getMessage(),e2);
                    }
                }catch (IllegalAccessException e3) {
                    if (logger.isWarnEnabled()){
                        logger.warn(e3.getMessage(),e3);
                    }
                }

                Ihr360Session session = Ihr360SessionContextHolder.getSession();
                if (session!=null){
                    AuthUserInfo userInfo = session.getUserInfo();
                    if (userInfo!=null){
                        String companyId = userInfo.getCompanyId();
                        if (!StringUtils.isEmpty(companyId)){
                            try{
                                BeanUtils.copyProperty(o,descriptor.getName(),companyId);
                            }catch (IllegalAccessException e1){
                                if (logger.isWarnEnabled()){
                                    logger.warn(e1.getMessage(),e1);
                                }
                            }catch (InvocationTargetException e2){
                                if (logger.isWarnEnabled()){
                                    logger.warn(e2.getMessage(),e2);
                                }
                            }
                        }
                    }
                }
                break;
            }
        }

        logger.info(o.toString());
    }
}